﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using cbmobile.DAC;
using PQT.API;
using cbmobile.BLC;

namespace cbmobile.BLC
{
    public class BLC_HangSanXuat
    {
        private CBMobileEntities da = null;

        public BLC_HangSanXuat()
        {   
            da = new CBMobileEntities();
        }


        public IList<HangSanXuat> All_Category()
        {
            return da.HangSanXuats.OrderBy(z => z.HangSX_ID).OrderBy(z => z.TenHangSX).ToList();
        }


        public HangSanXuat Get_HangSanXuat_byId(int HangSanXuat_ID)
        {
            IList<HangSanXuat> list = da.HangSanXuats.Where(p => p.HangSX_ID == HangSanXuat_ID).ToList();
            if (list.Count() == 0)
                return null;
            return list.First();
        }


        public IList<HangSanXuat> Rows_HangSanXuat(int p_page, int p_pageSize, string p_name)
        {
            int preCount = (p_page - 1) * p_pageSize;
            IList<HangSanXuat> list = da.HangSanXuats.Where(z => z.TenHangSX.Contains(p_name)).OrderByDescending(z => z.TenHangSX).Skip(preCount).Take(preCount + p_pageSize).ToList();
            return list;
        }


        public int GetTotalRecord(string p_name)
        {
            return da.HangSanXuats.Where(z => z.TenHangSX.Contains(p_name)).Count();
        }


        public void Delete(int HangSX_ID)
        {
            var existing = da.HangSanXuats.Where(p => p.HangSX_ID == HangSX_ID).First();
            existing.TrangThai = (int)CommonStatus.Delete;
            da.SaveChanges();
        }

        #region Create
        public void Create(int Parent_ID, string TenHangSX, int ThuTu, int TrangThai)
        {
            var objEnt = new HangSanXuat();
            objEnt.Parent_ID = Parent_ID;
            objEnt.TenHangSX = TenHangSX;
            objEnt.ThuTu = ThuTu;
            objEnt.TrangThai = TrangThai;

            da.HangSanXuats.AddObject(objEnt);
            da.SaveChanges();
        }

        public void Create(HangSanXuat role)
        {
            da.HangSanXuats.AddObject(role);
            da.SaveChanges();
        }
        #endregion


        #region Update
        public void Update(int HangSX_ID, int Parent_ID, string TenHangSX, int ThuTu, int TrangThai)
        {
            var role = new HangSanXuat();
            var objEnt = da.HangSanXuats.Where(p => p.HangSX_ID == HangSX_ID).First();
            objEnt.Parent_ID = Parent_ID;
            objEnt.TenHangSX = TenHangSX;
            objEnt.ThuTu = ThuTu;
            objEnt.TrangThai = TrangThai;
            

            da.SaveChanges();
        }
        #endregion

    }
}